package com.example.news.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.news.pojo.entity.Role;
import com.example.news.pojo.form.RoleForm;
import com.example.news.pojo.form.RolePageQuery;
import com.example.news.pojo.vo.RoleVo;
import com.example.news.service.RoleService;
import com.example.news.mapper.RoleMapper;
import com.example.news.utils.result.PageResult;
import com.example.news.utils.result.Result;
import com.example.news.utils.result.ResultCodeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
* @author 86147
* @description 针对表【role(角色表)】的数据库操作Service实现
* @createDate 2023-10-18 21:55:05
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Result<RoleVo> getRoleById(Integer id){
        Role role = roleMapper.selectById(id);
        if(role !=null){
            RoleVo roleVo = new RoleVo();
            BeanUtils.copyProperties(role,roleVo);
            return Result.ok(roleVo,"成功获取！");
        }
        return Result.build(null, ResultCodeEnum.SYSTEM_ERROR);
    }


    @Override
    public PageResult<RoleVo> getRoleList(RolePageQuery rolePageQuery){
        Page<Role> rolePage = new Page<>(rolePageQuery.getPageNum(),rolePageQuery.getPageSize());
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(rolePageQuery.getStatus() != null,Role::getStatus,rolePageQuery.getStatus())
                .like(rolePageQuery.getKeywords()!=null,Role::getName,rolePageQuery.getKeywords())
                .like(rolePageQuery.getKeywords()!=null,Role::getCode,rolePageQuery.getKeywords())
                .orderByAsc(Role::getSort)
                .orderByDesc(Role::getUpdateTime);
        IPage<Role> roleIPage = this.page(rolePage,queryWrapper);

        if(roleIPage != null){
            // 最终返回分页对象
            IPage<RoleVo> convertPage = roleIPage.convert(result -> {
                RoleVo vo = new RoleVo();
                BeanUtils.copyProperties(result, vo);
                return vo;
            });
            return PageResult.success(convertPage);
        }

        return PageResult.build(null, ResultCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public Result<String> addRole(RoleForm roleForm){
        LambdaQueryWrapper<Role> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Role::getCode,roleForm.getCode());
        Role role = roleMapper.selectOne(queryWrapper);
        if(role == null){
         Role newRole = new Role();
         BeanUtils.copyProperties(roleForm,newRole);
         this.save(newRole);
         return Result.ok(null,"成功添加！");
        }

        return Result.build(null, ResultCodeEnum.SYSTEM_ERROR);
    }

    @Override
    public Result<String> updateRole(RoleForm roleForm){
        Role role = roleMapper.selectById(roleForm.getId());
        if(role !=null){
            BeanUtils.copyProperties(roleForm,role);
            role.setUpdateTime(new Date());
            this.updateById(role);
            return Result.ok(null,"修改成功！");

        }
        return Result.build(null, 500,"角色不存在！");
    }

    @Override
    public Result<String> deleteRoleById(Integer id){
        Role role = roleMapper.selectById(id);
        if(role !=null){
            if(roleMapper.deleteById(id) == 1){
                return Result.ok(null,"成功删除！");
            }else{
                return Result.build(null, ResultCodeEnum.SYSTEM_ERROR);
            }

        }
        return Result.build(null, 500,"角色不存在！");
    }

}




